package com.ahu.qifei.fenghuolun.task;

import com.ahu.qifei.common.common.excetpion.ApiException;
import com.ahu.qifei.common.service.IFenghuolunService;
import com.ahu.qifei.common.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author ShiQifei
 * @date 2020/11/25 13:26、
 * 风火轮任务
 */

@Configuration
@EnableScheduling
@Slf4j
public class FenghuolunTask {
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Autowired
    IFenghuolunService fenghuolunService;

    @Autowired
    @Qualifier("mailServiceImpl")
    IMailService mailService;
    /**
     * 每个工作日9点签到
     */
    @Scheduled(cron="0 0 10 ? * MON-FRI")
    public void signIn() {
        String todayDate = LocalDate.now().format(dtf);
        try {
            Boolean success = fenghuolunService.signIn("80092929", "songYUAN2324");
            if(success) {
                log.info("80092929风火轮签到成功 {}", todayDate);
            }
        } catch(ApiException e) {
            log.error(e.getMessage(), e);
            // 邮件告警
            String[] to = {"714367321@qq.com"};
            mailService.sendMessage(to, "风火轮签到异常-" + todayDate, e.getMessage());
        }

    }

    /**
     * 每个工作日9点签到
     */
    @Scheduled(cron="0 5 10 ? * MON-FRI")
    public void signIn2() {
        String todayDate = LocalDate.now().format(dtf);
        try {
            Boolean success = fenghuolunService.signIn("80092923", "Cy994810..");
            if(success) {
                log.info("80092923风火轮签到成功 {}", todayDate);
            }
        } catch(ApiException e) {
            log.error(e.getMessage(), e);
            // 邮件告警
            String[] to = {"154569836@qq.com"};
            mailService.sendMessage(to, "风火轮签到异常-" + todayDate, e.getMessage());
        }
    }
}
